package com.example.e_book.Adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.e_book.Activity.ReadActivity;
import com.example.e_book.DAO.BookshelfDAO;
import com.example.e_book.R;
import com.example.e_book.entity.Book;
import com.example.e_book.entity.User;

import java.util.List;

public class MyBookAdapter extends RecyclerView.Adapter<MyBookAdapter.MyBookViewHolder> {

    private List<Book> bookList;
    private Context context;
    private User user;

    public MyBookAdapter(List<Book> bookList, Context context, User user) {
        this.bookList = bookList;
        this.context=context;
        this.user=user;
    }

    @NonNull
    @Override
    public MyBookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.my_book, parent, false);
        return new MyBookViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull MyBookViewHolder holder, int position) {
        Book book = bookList.get(position);
        holder.titleTextView.setText(book.getTitle());
        holder.authorTextView.setText("作者：" + book.getAuthor());
        holder.categoryTextView.setText("类别：" + book.getCategory());
        // 使用 Glide 加载书籍封面图片
        String coverImagePath = "file:///android_asset/" + book.getCoverImagePath();
        Glide.with(holder.itemView.getContext()).load(coverImagePath).into(holder.coverImageView);
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 从当前上下文（context）启动ReadActivity，并传递书籍的内容路径
                Intent intent = new Intent(holder.itemView.getContext(), ReadActivity.class);
                intent.putExtra("CONTENT_PATH", book.getContentPath());
                holder.itemView.getContext().startActivity(intent);
            }
        });
        holder.delButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理移出书架的逻辑
                removeBookFromShelf(book.getBookId(), user.getId(), holder.getAdapterPosition());
            }
        });
    }

    private void removeBookFromShelf(int bookId, int userId, int position) {
        BookshelfDAO bookshelfDAO = new BookshelfDAO(context, user);
        bookshelfDAO.removeBookFromShelf(bookId, userId);
        bookList.remove(position);
        notifyItemRemoved(position);
        notifyItemRangeChanged(position, bookList.size());
    }

    @Override
    public int getItemCount() {
        return bookList.size();
    }

    public void updateList(List<Book> newBookList) {
        this.bookList = newBookList;
    }

    public static class MyBookViewHolder extends RecyclerView.ViewHolder {
        ImageView coverImageView;
        TextView titleTextView;
        TextView authorTextView;
        TextView categoryTextView;
        Button delButton;

        public MyBookViewHolder(View itemView) {
            super(itemView);
            coverImageView = itemView.findViewById(R.id.coverImageView);
            titleTextView = itemView.findViewById(R.id.titleTextView);
            authorTextView = itemView.findViewById(R.id.authorTextView);
            categoryTextView = itemView.findViewById(R.id.categoryTextView);
            delButton = itemView.findViewById(R.id.delbutton);
        }
    }
}
